package com.example.demo2.core.page;

import java.util.regex.Pattern;

public class PageMessage {

	/**
	 * 第几页(默认第一页)
	 */
	private int pageNum = 1;

	/**
	 * 每页显示多少条记录(默认每页10条)
	 */
	private int pageSize = 10;

	/**
	 * 总记录数
	 */
	private long total;

	/**
	 * 总记录数
	 */
	private int pages;

	/**
	 * 前一页
	 */
	private int prePage;

	/**
	 * 下一页
	 */
	private int nextPage;
	/**
	 * 标准查询有效， 实例： updatedate desc, name asc
	 */
	private String orderBy = "";

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getPrePage() {
		if(this.pageNum <=1){
			this.prePage = 1;
		}else{
			this.prePage = this.pageNum - 1;
		}
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		if(this.pageNum>=this.pages){
			this.nextPage = this.pages;
		}else{
			this.nextPage = this.pageNum + 1;
		}
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}


	/**
	 * 获取查询排序字符串
	 * @return
	 */
	public String getOrderBy() {
		// SQL过滤，防止注入
		String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
				+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|limit)\\b)";
		Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
		if (sqlPattern.matcher(orderBy).find()) {
			return "";
		}
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
}
